﻿using IDal;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Model.DTO;
using Model.House;
using Model.Order;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Yitter.IdGenerator;

namespace Dal
{
    public class HouseOrderDal:IHouseOrderDal
    {
        private readonly HotelDbContext db;

        public HouseOrderDal(HotelDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="houseOrder">订单表</param>
        /// <returns>返回受影响行数</returns>
        public int AddOrder(HouseOrder houseOrder)
        {
            //预定编号请使用R_时间戳
            //houseOrder.HouseOrderNumber = "R_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            //雪花ID
            houseOrder.HouseOrderNumber = YitIdHelper.NextId().ToString();
            db.HouseOrders.Add(houseOrder);
            return db.SaveChanges();
        }
        /// <summary>
        /// 订单的显示+查询+分页
        /// </summary>
        /// <param name="loginname">登录人</param>
        /// <param name="max">时间最大值</param>
        /// <param name="min">时间最小值</param>
        /// <param name="name">入住人进行模糊查询</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="pagesize">每页显示的条数</param>
        /// <returns></returns>
        public dynamic ShowOrder(string loginname = "", DateTime? max = null, DateTime? min = null, string? name = "", int pageindex = 1, int pagesize = 3)
        {
            IQueryable<HouseOrderDto> list = from a in db.HouseOrders
                                             join b in db.Houses
                                             on a.HouseId equals b.HouseId
                                             select new HouseOrderDto()
                                             {
                                                 OiderId = a.OiderId,
                                                 HouseOrderNumber = a.HouseOrderNumber,
                                                 HouseId = a.HouseId,
                                                 CreateTime = a.CreateTime,
                                                 EndTime = a.EndTime,
                                                 UserName = a.UserName,
                                                 UserPhone = a.UserPhone,
                                                 UserNeed = a.UserNeed,
                                                 BreakFood = a.BreakFood,
                                                 UserImg = a.UserImg,
                                                 OrderTime = a.OrderTime,
                                                 OrderState = a.OrderState,
                                                 Name = a.Name,
                                                 CodeState = a.CodeState,
                                                 HouseName = b.HouseName,
                                                 HotelId = b.HotelId,
                                                 HouseTypeId = b.HouseTypeId,
                                                 HouseImg = b.HouseImg,
                                                 HaveWindow = b.HaveWindow,
                                                 HaveWifi = b.HaveWifi,
                                                 Arec = b.Arec,
                                                 People = b.People,
                                                 HousePrice = b.HousePrice
                                             };
            //逻辑删除只展示true
            list = list.Where(x => x.CodeState == true);
            //娃哈哈
            //登录人获取自己的订单
            list = list.Where(x => x.Name.Equals(loginname));
            //订单创建时间进去条件查询
            //要大于等于最小值
            if (min != null)
            {
                list = list.Where(x => x.CreateTime >= min);
            }
            //小于等于最大值
            if (max != null)
            {
                list = list.Where(x => x.CreateTime <= max);
            }
            if (!string.IsNullOrEmpty(name))
            {
                //Equals代表精确查询
                //Contains代表模糊查询
                list = list.Where(x => x.UserName.Contains(name));
            }
            return new
            {
                //总条数
                listcount = list.Count(),
                // Skip跳过
                //Take连续
                rows = list.OrderBy(x => x.OiderId).Skip((pageindex - 1) * pagesize).Take(pagesize)
                //rows = list.OrderByDescending(x => x.UserName).Skip((pageindex - 1) * pagesize).Take(pagesize)
            };
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>返回受影响行数</returns>
        public int UpdateCodeState(int id)
        {
            //通过主键id获取到整条数据
            HouseOrder houseOrder = db.HouseOrders.Find(id);
            houseOrder.CodeState = false;
            return db.SaveChanges();
        }
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="orderid">订单id</param>
        /// <param name="phone">通过手机号获取验证码</param>
        /// <returns>验证码</returns>
        public int Code(int orderid, string phone)
        {
            HouseOrder order = db.HouseOrders.Find(orderid);
            if (order.UserPhone == phone)
            {
                Random random = new Random();
                return random.Next(1000, 10000);
            }
            return -1;
        }
        /// <summary>
        /// 办理入住
        /// </summary>
        /// <param name="code">手动输入的验证码</param>
        /// <param name="codes">自动生成的验证啊</param>
        /// <param name="phone">手机号</param>
        /// <returns>返回受影响行数</returns>
        public int CheckIn(int code, int codes, string phone = "")
        {
            if (code != codes)
            {
                //验证码不匹配
                return -1;
            }
            Model.Order.CheckIn checkIn = new CheckIn()
            {
                UserPhone = phone,
                Code = code
            };
            db.CheckIns.Add(checkIn);
            return db.SaveChanges();
        }

    }
}
